﻿using UnityEngine;
using System.Collections;

public class BoundryBoxCollision : MonoBehaviour {

	private Vector3 bounds;

	private Vector2 leftWorldPosition;
	private Vector2 rightWorldPosition;

	void Start () 
	{
		bounds = this.renderer.bounds.size - (this.renderer.bounds.size / Mathf.Sqrt (2)) / 2;
		leftWorldPosition = Camera.main.ScreenToWorldPoint(new Vector3(bounds.x/2,0,0));
		rightWorldPosition = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width -  bounds.x/2,0,0));
	}

	// Update is called once per frame
	void Update () 
	{
		Vector3 screenPosition = Camera.main.WorldToScreenPoint(transform.position);
		if(screenPosition.x - bounds.x/2 < 0)
		{
			this.transform.localPosition = new Vector3(leftWorldPosition.x,this.transform.localPosition.y,this.transform.localPosition.z);
		}


		if(screenPosition.x >  Screen.width -  bounds.x/2)
		{
			this.transform.localPosition = new Vector3(rightWorldPosition.x,this.transform.localPosition.y,this.transform.localPosition.z);
		}


		if(screenPosition.y < 0)
		{
			//die die
			WorldCreator.Instance.endLevel();
		}

	}
}
